//
//  YGHomeViewController.swift
//  SwiftWebo
//
//  Created by 王勇 on 2018/5/26.
//  Copyright © 2018年 Brave. All rights reserved.
//

import UIKit
// 定义私有全局常量
private let cellId = "cellid"

class YGHomeViewController: YGBaseViewController {

    ///数据源数组
    private lazy var listData = [String]()
    
    ///加载数据
    override func loadData() {
       
        // 网络请求加载
       
//        YGNetworkManager.sharedManager.NomalRequst(URLString: nil, parameters: nil) { (, ) in
//            
//        }
        //延迟加载数据
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
            
            for i in 0..<15 {
                if self.isPullup {
                 //将数据追加数组底部
                   self.listData.append("上拉\(i)")
                }else{
                //将数据插入数组顶部
                self.listData.insert(i.description, at: 0)
            }
        }
           //结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复上拉
            self.isPullup = false
            //刷新表格
            self.tableView?.reloadData()
        }
    }
    

    /// 显示好友
    @objc private func showFriends(){
        print(#function)
        let vc = YGDomeViewController()
        
        navigationController?.pushViewController(vc, animated: true)
        
    }
}

// MARK: - 表格代理方法
extension YGHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listData.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // 1.获取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        // 2.设置cell
        cell.textLabel?.text = listData[indexPath.row]
        // 3.返回cell
        return cell
    }
    
}


//MARK:- 设置界面
extension YGHomeViewController {
    
    //重写父类方法
    override func setupUI() {
        super.setupUI()
    //设置导航栏按钮
      navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, target: self, action: #selector(showFriends))
    
    // 注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
    
}
